﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace XWord.WPF.ApplicationFramework.Controls
{
    public class EditTextBox : Control
    {
        static EditTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(EditTextBox),
                new FrameworkPropertyMetadata(typeof(EditTextBox)));
        }

        private TextBlock _textBlock_Element;
        private TextBox _textBox_Element;

        public override void OnApplyTemplate()
        {
            _textBlock_Element = this.GetTemplateChild("TextBlock_Part") as TextBlock;
            _textBlock_Element.MouseLeftButtonDown += new MouseButtonEventHandler(_textBlock_Element_MouseLeftButtonDown);
            UIElementService.SetAttachEventState(_textBlock_Element, true);
            _textBox_Element = this.GetTemplateChild("TextBox_Part") as TextBox;
            _textBox_Element.KeyDown += new KeyEventHandler(_textBox_Element_KeyDown);
            _textBox_Element.LostFocus += new RoutedEventHandler(_textBox_Element_LostFocus);
            base.OnApplyTemplate();
        }

        void _textBlock_Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (!string.IsNullOrEmpty(_textBox_Element.Text))
                    _textBox_Element.SelectionStart = _textBox_Element.Text.Length;
                _textBox_Element.Focus();
            }), System.Windows.Threading.DispatcherPriority.Render);
        }

        void _textBox_Element_LostFocus(object sender, RoutedEventArgs e)
        {
            UIElementService.SetIsMouseDown(_textBlock_Element, false);
           // RaiseTextChangedEvent(this.Text);
        }

        void _textBox_Element_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                UIElementService.SetIsMouseDown(_textBlock_Element, false);
                RaiseTextChangedEvent(this.Text);
                //_textBox_Element.LostFocus -= _textBox_Element_LostFocus;

            }
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(EditTextBox), 
            new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

       
        public static readonly RoutedEvent TextChangedEvent = EventManager.RegisterRoutedEvent(
            "TextChanged", RoutingStrategy.Bubble, typeof(EditTextChangedEventHandler), typeof(EditTextBox));

        public event EditTextChangedEventHandler TextChanged
        {
            
            add { AddHandler(TextChangedEvent, value); }
            remove { RemoveHandler(TextChangedEvent, value); }
        }

        private void RaiseTextChangedEvent(string text)
        {
            EditTextChangedEventArgs newEventArgs = new EditTextChangedEventArgs() { 
                RoutedEvent = TextChangedEvent, Text=text };

            RaiseEvent(newEventArgs);
        }
    }

    public class EditTextChangedEventArgs : RoutedEventArgs
    {
        public string Text { get; set; }
    }

    public delegate void EditTextChangedEventHandler(object sender, EditTextChangedEventArgs e);
}
